#include "stdafx.h"

GameoverDialog::GameoverDialog(double x, double y)
	:AbstractDialog(x, y)
{
	ret = new Button("return");
	ret->setImages(
		"image/buttons/back.png",	
		"image/buttons/back_hover.png",
		"image/buttons/back_click.png"
	);
	ret->setMetrics(200, 149, 500, 140);

	restart = new Button("restart");
	restart->setImages(
		"image/buttons/restart.png",
		"image/buttons/restart_hover.png",
		"image/buttons/restart_click.png"
	);
	restart->setMetrics(300, 149, 0, 140);
}

GameoverDialog::~GameoverDialog()
{
	delete ret;
	delete restart;
}

bool GameoverDialog::mouseDown(int button, int x, int y)
{
	if (ret->hit(x, y))
	{
		ret->update(Button::DOWN);

		die = true;
		command = "StartupScene";
	}

	if (restart->hit(x, y))
	{
		restart->update(Button::DOWN);

		die = true;
		command = "GameScene";
	}

	return true;
}

bool GameoverDialog::mouseUp(int button, int x, int y)
{
	if (ret->hit(x, y))
	{
		ret->update(Button::HOVER);
	}

	if (restart->hit(x, y))
	{
		restart->update(Button::HOVER);
	}

	return true;
}

bool GameoverDialog::mouseMove(int button, int x, int y)
{
	if (ret->hit(x, y))
	{
		ret->update(Button::HOVER);
	}
	else
	{
		ret->update(Button::UP);
	}

	if (restart->hit(x, y))
	{
		restart->update(Button::HOVER);
	}
	else
	{
		restart->update(Button::UP);
	}

	return true;
}

bool GameoverDialog::render()
{
	static const double retLeft = 250;
	static const double retTop = 530;
	static const double restartLeft = 570;
	static const double restartTop = 530;
	static const double buttonWidth = 149;
	static const double buttonHeight = 140;

	background->render(x, y, scale*background->getWidth(), scale*background->getHeight(), 0);
	
	int left = this->x - background->getWidth() / 2.0 * scale;
	int right = this->x + background->getWidth() / 2.0 * scale;
	int top = this->y - background->getHeight() / 2.0 * scale;
	int bottom = this->y + background->getHeight() / 2.0 * scale;

	ret->setMetrics(left + retLeft * scale, buttonWidth * scale, top + retTop * scale, buttonHeight * scale);
	ret->render();

	restart->setMetrics(left + restartLeft * scale, buttonWidth * scale, top + restartTop * scale, buttonHeight * scale);
	restart->render();

	return true;
}

bool GameoverDialog::update(double dtime)
{
	if (die)
	{
		return false;
	}

	time += dtime;

	if (time > time1)
	{
		scale = 1;
		acceptInput = true;
	}
	else if (time > time0)
	{
		scale = (time0 - time) / (time1 - time0)*0.2 + 1.2;
	}
	else if (time > 0)
	{
		scale = 0.7*time + 0.5;
	}

	return true;
}

bool GameoverDialog::hit(int x, int y)
{
	if (!acceptInput)
	{
		return false;
	}

	int left = this->x - background->getWidth() / 2;
	int right = this->x + background->getWidth() / 2;
	int top = this->y - background->getHeight() / 2;
	int bottom = this->y + background->getHeight() / 2;

	if (x >= left && x <= right && y >= top && y <= bottom)
	{
		return true;
	}
	else
	{
		return false;
	}
}